﻿using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class FareOffices : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                lblError.Text = "";
                return;
            }
            else
            {
                if (Session["ACL"].ToString().IndexOf("FA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["code"] != null)
                {
                    lblFareCode.Text = Request.QueryString["code"];
                    hidFareCode.Value = lblFareCode.Text;
                }
                if (Request.QueryString["airline"] != null)
                {
                    lblFareAirline.Text = Request.QueryString["airline"];
                    hidFareAirline.Value = lblFareAirline.Text;
                }
            }
            butAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            butDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void lstAirline_SelectedIndexChanged(object sender, EventArgs e)
        {
            //hidAuthAirline.Value = lstAirline.SelectedValue;
            lstAvailOffices.DataBind();
        }
        protected void butAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FA5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            int ret_code;
            foreach (ListItem item in lstAvailOffices.Items)
            {
                if (item.Selected)
                {
                    try
                    {
                        cmd.Parameters.Clear();
                        cmd.CommandText = "bll_fare_add_office";
                        para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                        para = cmd.Parameters.AddWithValue("@fare_code", hidFareCode.Value);
                        para = cmd.Parameters.AddWithValue("@auth_airline", lstAirline.SelectedValue);
                        para = cmd.Parameters.AddWithValue("@auth_office", item.Value);
                        //para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                        para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                        para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                        ret_code = TinyDAL.Exec(ref cmd);
                        if (ret_code == 0)
                        {
                            lblError.Text = "Done.";
                            //lstAvailOffices.DataBind();
                            GridView1.DataBind();
                        }
                        else if (ret_code == 1)
                        {
                            lblError.Text = "Adding failed!";
                        }
                        else if (ret_code == 2)
                        {
                            lblError.Text = "Importer rejected!";
                        }
                        else if (ret_code == 3)
                        {
                            lblError.Text = "Fare not found!";
                        }
                        else if (ret_code == 4)
                        {
                            lblError.Text = "Can not edit!";
                        }
                        else if (ret_code == 5)
                        {
                            lblError.Text = "This is a PUBLIC fare!";
                        }
                        else
                        {
                            lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            lblError.Text = ex.Message;
                        else
                            lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                    }
                }
            }
            lstAvailOffices.DataBind();

        }
        protected void butDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FA5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            SqlCommand cmd = new SqlCommand();
            int ret_code;
            lblError.Text = "";
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            try
            {
                cmd.CommandText = "bll_fare_delete_office";
                SqlParameter para = cmd.Parameters.AddWithValue("@fare_code", hidFareCode.Value);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@auth_airline", strArray[0]);
                para = cmd.Parameters.AddWithValue("@auth_office", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    lstAvailOffices.DataBind();
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                {
                    lblError.Text = "Addint failed";
                }
                else if (ret_code == 2)
                {
                    lblError.Text = "Importer rejected!";
                }
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                return;
            }


        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lstAvailOffices.Items.Clear();
            //if (lstAirline.SelectedValue == "*")
            //{
            //    ListItem item = new ListItem("* - (All public agents)", "*");
            //    lstAvailOffices.Items.Add(item);
            //}
            //else
                lstAvailOffices.DataBind();
        }
        //protected void lstAirline_DataBound(object sender, EventArgs e)
        //{
        //    lstAirline.Items.Add(new ListItem("* - (Public)", "*"));
        //    lstAirline.SelectedValue = Session["AIRLINE"].ToString();
        //    lstAvailOffices.DataBind();
        //}
        protected void lstAvailOffices_DataBound(object sender, EventArgs e)
        {
            lstAvailOffices.Items.Add(new ListItem("* - (All offices) ", "*"));
        }
    }
}